//
//  XLVisitorView.swift
//  XLWeiBo
//
//  Created by 李晓龙 on 16/9/20.
//  Copyright © 2016年 lixiaolong. All rights reserved.
//

import UIKit

//定义代理协议
//protocol XLVisitorViewDelegate: NSObjectProtocol {
//    func didSelectedLogin()
//}

class XLVisitorView: UIView {

    //需要传入执行的闭包
    var loginClosure: (() -> ())?
    
    //定义代理对象
//    weak var delegate: XLVisitorViewDelegate?
    
    //MARK: --懒加载
    //旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //未登陆信息
    private lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        //居中
        label.textAlignment = .center
        //多行
        label.numberOfLines = 0

        return label
    }()
    
    //登陆按钮
     private lazy var loginButton: UIButton = {
    
        let button = UIButton()
        //添加按钮点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登陆", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable")?.withRenderingMode(.alwaysOriginal), for: .normal)
//        button.backgroundColor = UIColor.white
//        button.layer.cornerRadius = 4
//        button.layer.masksToBounds = true
        //去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        
    
        return button
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        //添加按钮的点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
//        button.backgroundColor = UIColor.white
//        button.layer.cornerRadius = 4
//        button.layer.masksToBounds = true
        
        //去掉按钮点击高亮效果
        button.adjustsImageWhenHighlighted = false
        
        return button
        
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //设置背景色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //添加到视图
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //设置约束
        cycleImageView .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.bottom.equalTo(loginButton)
            make.width.equalTo(100)
        }
        
        
        
        
        
        
        
    }

    //完善访客视图信息
    func updateVisitorInfo(message: String?,imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            
            //如果代码执行到这，表示消息，发现，我的视图
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName )
            //隐藏
            cycleImageView.isHidden = true
            
        }else{
            //执行旋转动画
            starAnimation()
        }
        
    }
    
    //开启动画
    private func starAnimation() {
        //创建核心动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //执行的路径
        animation.toValue = 2 * M_PI
        //动画时长
        animation.duration = 20
        //动画执行的次数
        animation.repeatCount = MAXFLOAT
        //失去焦点不被释放
        animation.isRemovedOnCompletion = false
        //添加到layer
        cycleImageView.layer.add(animation, forKey: nil)
        
        
        
    }
    
    
    //MARK: --点击事件
    @objc private func loginAction() {
        
//        print("访客视图点击的操作")
        
        //执行闭包
        loginClosure?()
        
        //通过代理对象执行代理方法
//        delegate?.didSelectedLogin()
        
    }
    
    
    
    
    
    
    
    
    
    
    
}
